package com.gali.mall.service.impl;

import com.gali.mall.exception.GaLiException;
import com.gali.mall.exception.GaLiExceptionEnum;
import com.gali.mall.model.dao.CategoryMapper;
import com.gali.mall.model.pojo.Category;
import com.gali.mall.model.vo.CategoryVO;
import com.gali.mall.request.categoryReq.AddCategoryReq;
import com.gali.mall.request.categoryReq.UpdateCategoryReq;
import com.gali.mall.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
   private CategoryMapper categoryMapper;
    /**
     * 增
     *
     * @param addCategoryReq
     * @return
     */
    @Override
    public Boolean addCategory(AddCategoryReq addCategoryReq) {
        Category category = new Category();
        Category categoryOld = categoryMapper.seleteByName(addCategoryReq.getName());
        if (categoryOld!=null){
            throw new GaLiException(GaLiExceptionEnum.NAME_EXITS);
        }
       BeanUtils.copyProperties(addCategoryReq,category);
        return categoryMapper.insertSelective(category)>0;
    }

    /**
     * 删
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteCategory(Integer id) {
        Category category = categoryMapper.selectByPrimaryKey(id);
        if (category==null){
            throw  new GaLiException(GaLiExceptionEnum.DELETE_ERROR);
        }
        return categoryMapper.deleteByPrimaryKey(id)>0;
    }

    /**
     * 改
     *
     * @param updateCategoryReq
     * @return
     */
    @Override
    public Boolean updateCategory(UpdateCategoryReq updateCategoryReq) {
        Category category = new Category();
        BeanUtils.copyProperties(updateCategoryReq,category);
        return categoryMapper.updateByPrimaryKeySelective(category)>0;
    }

    /**
     * 查询全部
     *
     * @return
     */
    @Override
    public List<Category> findAll() {
        return categoryMapper.seleteAll();
    }

    /**
     * 根据id查询商品
     *
     * @param id
     * @return
     */
    @Override
    public Category findCategoryById(Integer id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据级别查询商品
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> findCategoryByType(Integer type) {
        return categoryMapper.seleteByType(type);
    }

    /**
     * 根据名字模糊查询
     *
     * @param name
     * @return
     */
    @Override
    public List<Category> findCategoryFromName(String name) {
        return categoryMapper.seleteFromName(name);
    }

    /**
     * 查询分页（管理员）
     *
     * @param PageNumber
     * @param pageSize
     */
    @Override
    @Cacheable(value = "listForAdmin")
    public PageInfo listForAdmin(Integer PageNumber, Integer pageSize) {
        PageHelper.startPage(PageNumber,pageSize,"type,order_num");
        List<Category> list =categoryMapper.seleteAll();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * 查询分页（用户）
     *
     * @return
     */
    @Override
    @Cacheable(value = "listForConsumer")
    public List<CategoryVO> listForCustomer(Integer parentId){
        ArrayList<CategoryVO> categoryVOList = new ArrayList<>();
        recursiveFindCategories(categoryVOList,parentId);
        return categoryVOList;
    }
    //递归方法获取目录树
    private void recursiveFindCategories(List<CategoryVO> categoryVOList,Integer parentId){
        //递归获取所有子类别，并组合成为一个“目录树”
        List<Category> categoryList = categoryMapper.selectForParentId(parentId);
        if(!CollectionUtils.isEmpty(categoryList)){
            for (int i = 0; i < categoryList.size(); i++) {
                Category category = categoryList.get(i);
                CategoryVO categoryVO = new CategoryVO();
                BeanUtils.copyProperties(category,categoryVO);
                categoryVOList.add(categoryVO);
                recursiveFindCategories(categoryVO.getCategoryVoList(),categoryVO.getId());
            }
        }
    }

}
